﻿using KendoMvcUI.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KendoMvcUI.Enums;
using System.Web.Mvc;
using KendoMvcUI.Common;

namespace KendoMvcUI.UI
{
    public class AutoComplete : KendoUIBase
    {
        public AutoComplete(KendoUIControlSettings settings, ViewContext context, IViewDataContainer viewDataContainer)
            : base(settings, context, viewDataContainer)
        {

        }
        public override string RenderHtml()
        {
            return "<input id='{0}'/>".Formator(Settings.Id);
        }
    }

    public class AutoCompleteSettings : KendoBindingBase
    {
        public AutoCompleteSettings()
            : base("kendoAutoComplete")
        {   //设置属性的默认值
            this.Filter = FilterString.startsWith;
            Placeholder = "请输入值...";
        }
        /// <summary>
        /// 
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript=true)]
        public Action<AnimationSettings> Animation { get; set; }
        #region 属性
        
        public bool? Enable { get; set; }
        [PropertySetting(IsIngoreForJavaScript=true)]
        public FilterString? Filter { get; set; }
        /// <summary>
        /// If set to true the first suggestion will be automatically highlighted.
        /// </summary>
        public bool? HighlightFirst { get; set; }
        /// <summary>
        /// If set to false case-sensitive search will be 
        /// performed to find suggestions. The widget performs case-insensitive searching by default
        /// </summary>
        public bool? IgnoreCase { get; set; }
        /// <summary>
        /// The minimum number of characters the user must type 
        /// before a search is performed. Set to higher value than 1 
        /// if the search could match a lot of items.
        /// </summary>
        public int? MinLength { get; set; }
        public string Placeholder { get; set; }
        public string Separator { get; set; }
        /// <summary>
        /// If set to true the widget will automatically use the first suggestion as its value.
        /// </summary>
        public bool? Suggest { get; set; }
        /// <summary>
        /// '<div><h2>Fruits</h2></div>'
        /// </summary>
        public string HeaderTemplate { get; set; }
        /// <summary>
        /// The template used to render the suggestions. By default the widget displays 
        /// only the text of the suggestion (configured via dataTextField).
        /// eg:'<span><img src="/img/#: id #.png" alt="#: name #" />#: name #</span>'
        /// </summary>
        public string Template { get; set; }
        #endregion
        #region 事件
        /// <summary>
        /// The event handler function context 
        /// (available via the this keyword) will be set to the widget instance
        /// 参数e
        /// </summary>
        //[PropertySetting(IsIngoreForJavaScript=true)]
        public string On_Change { get; set; }
        public string On_Close { get; set; }
        /// <summary>
        /// 参数e
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript=true)]
        public string OnDataBound { get; set; }
        /// <summary>
        /// 参数e
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnFiltering { get; set; }
        /// <summary>
        /// 参数e
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnOpen { get; set; }
        /// <summary>
        /// 参数e
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript = true)]
        public string OnSelect { get; set; }
        #endregion

        public override string ToJsSettings()
        {
            var js=base.ToJsSettings();

            if (!js.EndsWith(","))
                js += ",";
            if (Animation != null)
            {
                var animation = new AnimationSettings();
                Animation(animation);
                js += animation.ToString() + ",";
            }
                
            if (Filter.HasValue)
                js += "filter:'{0}',".Formator(Filter.Value.ToString());

            //if (!OnChange.IsNullOrWhiteSpace())
            //    js += "change:{0},".Formator(OnChange);
            //if (!OnClose.IsNullOrWhiteSpace())
            //    js += "close:{0},".Formator(OnClose);
            //if (!OnDataBound.IsNullOrWhiteSpace())
            //    js += "dataBound:{0},".Formator(OnDataBound);
            //if (!OnFiltering.IsNullOrWhiteSpace())
            //    js += "filtering:{0},".Formator(OnFiltering);
            //if (!OnOpen.IsNullOrWhiteSpace())
            //    js += "open:{0},".Formator(OnOpen);
            //if (!OnSelect.IsNullOrWhiteSpace())
            //    js += "select:{0},".Formator(OnSelect);

            return js.TrimEnd(',');
        }
    }
}
